package com.zzw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.bean.Content;
import com.zzw.bean.Msg;
import com.zzw.service.ContentService;
import com.zzw.utils.ExcelUtil;
import com.zzw.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 * 内容管理控制器
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Api(tags = "内容管理接口")
@RequestMapping("/content")
@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;

    @ApiOperation(value = "内容列表页面", notes = "跳转到内容列表页面")
    @RequestMapping("/list")
    public String list() {
        return "content/list";
    }

    @ApiOperation(value = "保存内容", notes = "保存内容信息")
    @RequestMapping("/save")
    @ResponseBody
    public Msg save(Content content) {
        contentService.save(content);
        return Msg.success();
    }

    @ApiOperation(value = "删除内容", notes = "根据ID删除内容")
    @ApiImplicitParam(name = "id", value = "内容ID", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("delete")
    @ResponseBody
    public Msg delete(@RequestParam("id") Integer id) {
        contentService.deleteById(id);
        return Msg.success();
    }

    @ApiOperation(value = "更新内容", notes = "更新内容信息")
    @RequestMapping("update")
    @ResponseBody
    public Msg update(Content content) {
        contentService.update(content);
        return Msg.success();
    }


    //带条件的分页查询
    @ApiOperation(value = "内容列表数据", notes = "根据条件查询内容列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "query", defaultValue = "10")
    })
    @RequestMapping("listByCondition")
    @ResponseBody
    public Msg listByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               Content content) throws ParseException {

        PageHelper.startPage(pageNum, pageSize);

        List<Content> contents = contentService.listByCondition(content);

        //将分页查询的结果, 封装到PageInfo
        //PageInfo对象包含了分页的各个信息, 比如当前页面pageNum,共有多少记录
        PageInfo pageInfo = new PageInfo(contents, pageSize);

        //将pageInfo封装到Msg对象
        return Msg.success().add("pageInfo", pageInfo);
    }


    @ApiOperation(value = "导出Excel", notes = "导出内容数据到Excel文件")
    @RequestMapping("/export")
    public void exportExcel(Content content, HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取数据
        List<Content> contents = contentService.selectContent(content);

        // 定义表头
        String[] headers = {"ID", "文件名称", "创建时间", "下载次数"};

        // 定义表头与字段的映射关系
        LinkedHashMap<String, String> headerFieldMap = new LinkedHashMap<>();
        headerFieldMap.put("id", "ID");
        headerFieldMap.put("rejectReason", "驳回原因");
        headerFieldMap.put("createTime", "创建时间");

        // 调用工具类进行导出
        ExcelUtil.exportExcel(request,response, headerFieldMap, contents,
                "病例审核.xlsx", "病例审核");
    }
}
